package Model.Spaceship;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;

import Model.Fighter;
import Model.MarketPlace.MarketPlaceItem;

/**
 * The Space Ship class for player to travel in
 *
 */
public class Spaceship implements Serializable{
	/**
	 * Name of space ship
	 */
	protected String name;

	/**
	 * list of items in cargo
	 */
	protected List<MarketPlaceItem> cargoItems;

	/**
	 * capacity of cargo
	 */
	protected final int cargo;

	/**
	 * number of items currently in cargo
	 */
	protected int currentCargo;

	/**
	 * fuel in space ship
	 */
	protected double fuel;

	/**
	 * health of space ship
	 */
	protected int health;

	/**
	 * damage this ship can deal
	 */
	protected int damage;

	/**
	 * defense of ship
	 */
	protected int defense;

	/**
	 *  Fuel capacity of ship
	 */
	protected double maxFuel = 100;
	
	/**
	 * gets defense
	 * @return defense
	 */
	protected int getDefense() {
		return defense;
	}

	/**
	 * sets defense
	 * @param defense
	 */
	protected void setDefense(int defense) {
		this.defense = defense;
	}

	/**
	 * A fighter associated with ship
	 */
	protected Fighter fighter;
	
	/**
	 * sets fighter
	 * @param fighter
	 */
	protected final void setFighter(Fighter fighter){
		this.fighter = fighter;
	}
	
	/**
	 * gets damage
	 * @return
	 */
	protected int getDamage() {
		return damage;
	}

	/**
	 * sets damage
	 * @param damage
	 */
	protected void setDamage(int damage) {
		this.damage = damage;
	}

	/**
	 * gets health
	 * @return
	 */
	public int getHealth() {
		return health;
	}

	/**
	 * sets health
	 * @param health
	 */
	public void setHealth(int health) {
		this.health = health;
	}
	
	/**
	 * attack action
	 * @return fast attack level of fighter
	 */
	public int attack(){
		return fighter.fastAttack();
	}

	/**
	 * Constructor
	 * @param name
	 * @param cargo
	 * @param fuel
	 * @param health
	 */
	protected Spaceship(String name, int cargo, double fuel, int health){
		this.name = name;
		this.cargo = cargo;
		this.cargoItems = new LinkedList<MarketPlaceItem>();
		this.currentCargo = cargo;
		this.fuel = fuel;
		this.health = health;
	}
	
	/**
	 * gets current cargo level
	 * @return currentCargo
	 */
	public int getCurrentCargo() {
		return currentCargo;
	}

	/**
	 * sets currentCargo level
	 * @param currentCargo
	 */
	public void setCurrentCargo(int currentCargo) {
		this.currentCargo = currentCargo;
	}

	/**
	 * gets cargo
	 * @return cargo
	 */
	public int getCargo() {
		return cargo;
	}

	/**
	 * gets fuel
	 * @return fuel
	 */
	public double getFuel() {
		return fuel;
	}
	
	/**
	 * gets maxFuel
	 * @return maxFuel
	 */
	public double getMaxFuel(){
	    return maxFuel;
	}

	/**sets fuel
	 * @param fuel
	 */
	public void setFuel(double fuel) {
		final DecimalFormat fmt = new DecimalFormat("0.00");
		try {
		    this.fuel = java.lang.Double.parseDouble(fmt.format(fuel));
		}catch(NumberFormatException e){
		    e.printStackTrace();
		}
		
	}

	/**
	 * gets name
	 * @return name
	 */
	public String getName(){
		return name;
	}

	/**
	 * gets cargo items
	 * @return list of all cargo items
	 */
	public List<MarketPlaceItem> getCargoItems() {
		return cargoItems;
	}

	/**adds marketplace item to the cargo of ship
	 * @param item
	 */
	public void addCargoItem(MarketPlaceItem item){
		cargoItems.add(item);
	}
	
	/**
	 * removes this marketplace item from the ship
	 * @param item
	 */
	public void removeCargoItem(MarketPlaceItem item){
		cargoItems.remove(item);
	}
}
